﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LevenBlog.Core.Entities
{
    [Flags]
    public enum FriendLinkState
    {
        None = 1,
        Image = 2,
        Show = 4,
        Index = 8,
        NoImage = 16,
        NoShow = 32,
        NoIndex = 64
    }

    public static class FriendLinkStateExtension
    {
        public static bool Test(this FriendLinkState state, FriendLinkState testState)
        {
            if ((state & testState) == state)
                return true;
            else
                return false;
        }

        public static string CreateSqlServerWhere(this FriendLinkState state, string innerWhere)
        {
            if (state.Test(FriendLinkState.None))
                return innerWhere;
            StringBuilder sb = new StringBuilder(innerWhere);
            if (state.Test(FriendLinkState.Image))
                sb.Append(" and [isimage]=1");
            if (state.Test(FriendLinkState.Index))
                sb.Append(" and [isindex]=1");
            if (state.Test(FriendLinkState.Show))
                sb.Append(" and [isshow]=1");
            if (state.Test(FriendLinkState.NoImage))
                sb.Append(" and [isimage]=0");
            if (state.Test(FriendLinkState.NoIndex))
                sb.Append(" and [isindex]=0");
            if (state.Test(FriendLinkState.NoShow))
                sb.Append(" and [isshow]=0");
            return sb.ToString();
        }

        public static string CreateSqlServerWhere(this FriendLinkState state)
        {
            return state.CreateSqlServerWhere(string.Empty);
        }
    }
}
